package composite.composite.impl;

import composite.composite.FileSystemComponent;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合节点 (Composite): 目录
 * @author Liu Yicong
 * @date 2025/9/23
 */
public class Directory implements FileSystemComponent {
	private String name;
	private List<FileSystemComponent> children = new ArrayList<>();

	public Directory(String name) {
		this.name = name;
	}

	// 组合节点特有的方法
	public void add(FileSystemComponent component) {
		children.add(component);
	}

	public void remove(FileSystemComponent component) {
		children.remove(component);
	}

	@Override
	public String getName() {
		return name;
	}

	// 递归计算目录总大小
	@Override
	public long getSize() {
		long totalSize = 0;
		for (FileSystemComponent child : children) {
			totalSize += child.getSize(); // 递归调用
		}
		return totalSize;
	}

	// 递归打印目录结构
	@Override
	public void print() {
		System.out.println("  + 目录: " + name + " (总大小: " + getSize() + " KB)");
		for (FileSystemComponent child : children) {
			child.print(); // 递归调用
		}
	}
}
